package com.techsen.tsweb.sys.web.controller;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.techsen.tsweb.core.domain.JsonResult;
import com.techsen.tsweb.core.util.SubjectUtil;
import com.techsen.tsweb.core.util.ValidUtil;
import com.techsen.tsweb.sys.domain.Oper;
import com.techsen.tsweb.sys.domain.User;
import com.techsen.tsweb.sys.service.OperService;
import com.techsen.tsweb.sys.util.SysConst;

@Controller
@RequestMapping("/sys/oper")
public class OperController {

    @Resource
    private OperService operService;

    /**
     * 根据菜单ID查找操作
     */
    @RequestMapping("/list-by-menuid")
    public @ResponseBody List<Oper> opers(String menuId) {
        List<Oper> retVal = new ArrayList<Oper>();
        if (ValidUtil.isValid(menuId)) {
            retVal = this.operService.getListByEntity(new Oper().setMenuId(menuId));
        }
        return retVal;
    }
    
    /**
     * 根据菜单ID和登录用户查找授权的操作
     */
    @RequestMapping("/list-authorized-opers-by-menuid")
    public @ResponseBody List<Oper> listAuthorizedOpersByMenuId(String menuId) {
        List<Oper> retVal = new ArrayList<Oper>();
        if (ValidUtil.isValid(menuId)) {
            retVal = this.operService.getListByEntity(new Oper().setMenuId(menuId));
            
            User user = (User) SubjectUtil.getSubject().getSession().getAttribute(SysConst.LOGIN_USER);
            List<String> ids = this.operService.getAuthorizedOperIdsByUserId(user.getId());
            if (ValidUtil.isValid(ids)) {
                Iterator<Oper> iter = retVal.iterator();
                while (iter.hasNext()) {
                    Oper oper = iter.next();
                    if (!ids.contains(oper.getId())) {
                        iter.remove();
                    }
                }
            }
        }
        return retVal;
    } 
    
    /**
     * 进入操作管理界面
     */
    @RequestMapping
    public String index(String menuId, Model model) {
        model.addAttribute("menuId", menuId);
        return "sys/oper";
    }
    
    /**
     * 根据控制器ID获取操作集合
     */
    @RequestMapping("/list-by-controllerid")
    public @ResponseBody List<Oper> listByControllerId(String controllerId) {
        List<Oper> list = new ArrayList<Oper>();
        if (ValidUtil.isValid(controllerId)) {
            list = this.operService.getListByEntity(new Oper().setControllerId(controllerId));
        }
        return list;
    }
    
    /**
     * 进入添加操作界面
     */
    @RequestMapping(value = "/add", method = RequestMethod.GET)
    public String add(String controllerId, Model model) {
        model.addAttribute("controllerId", controllerId);
        return "sys/oper-add";
    }
    
    /**
     * 添加操作
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public @ResponseBody JsonResult add(Oper oper) {
        this.operService.add(oper);
        return new JsonResult(oper);
    }
    
    /**
     * 删除操作
     */
    @RequestMapping("/delete")
    public @ResponseBody JsonResult delete(String id) {
        if (ValidUtil.isValid(id)) {
            this.operService.delete(id);
        }
        return new JsonResult(id);
    }
    
    /**
     * 进入修改操作界面
     */
    @RequestMapping(value = "/update", method = RequestMethod.GET)
    public String update(String operId, Model model) {
        Oper oper = this.operService.get(operId);
        model.addAttribute("oper", oper);
        return "sys/oper-update";
    }
    
    /**
     * 修改操作
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public @ResponseBody JsonResult update(Oper oper) {
        this.operService.update(oper);
        return new JsonResult(oper);
    }
    
}